// pages/home/device/status/index.js
import util from '../../../../utils/util.js';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        loaded: false,
        page: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const that = this;
        util.api.get('store/device/status', {page: this.data.page}, resp => {
            if (resp.status == true) {
                that.setData({
                    list: resp.data,
                    page: this.data.page + 1
                });
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    bindShowStoreDeviceListTap: function (e) {
        wx.navigateTo({
            url: '/pages/home/device/list/index?storeId=' + e.currentTarget.dataset.storeId,
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading();
        var that = this;
        this.setData({
            page: 1
        });

        util.api.get('store/device/status', {paga: this.data.page}, resp => {
            if (resp.status == true) {
                that.setData({
                    list: resp.data,
                    page: that.data.page + 1
                });
            }

            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        const that = this;
        if (this.data.loaded == true) {
            wx.showToast({
                title: '别拉了，到底了',
                icon: 'none'
            });
        } else {
            wx.showLoading({
                title: '玩命加载中',
            });

            util.api.get('store/device/status', { page: this.data.page }, resp => {
                if (resp.status == true) {
                    if (resp.data.length < 1) {
                        wx.showToast({
                            title: '没有更多数据了',
                            icon: 'none'
                        });

                        that.setData({
                            loaded: true
                        });
                    } else {
                        that.setData({
                            list: that.data.list.concat(resp.data),
                            page: that.data.page + 1
                        });
                    }
                }

                setTimeout(function () {
                    wx.hideLoading();
                }, 1500)
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})